package controller

import (
	"github.com/gin-gonic/gin"
	"hytx_manager/pkg/app"
	"github.com/astaxie/beego/validation"
	"github.com/Unknwon/com"
	"net/http"
	"hytx_manager/pkg/e"
	"hytx_manager/models"
	"hytx_manager/pkg/setting"
	"strings"
	"time"
	"strconv"
)

// 查看角色列表
func GetRoles(c *gin.Context) {
	name := c.Query("name")
	begin_time := c.Query("begin_time")
	end_time := c.Query("end_time")
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	limit, _ := strconv.Atoi(c.DefaultQuery("limit", strconv.Itoa(setting.AppSetting.PageSize)))
	roles, paginate := models.GetRoles(name, begin_time, end_time, page, limit)
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "ok",
		"data": gin.H{
			"paginate": paginate,
			"data":     roles,
		},
	})
}

func GetRolesNoPage(c *gin.Context)  {
	appG := app.Gin{c}
	name := c.Query("name")
	begin_time := c.Query("begin_time")
	end_time := c.Query("end_time")
	roles, err := models.GetRolesNoPage(name,begin_time, end_time)
	if err != nil {
		appG.Response(http.StatusOK, e.INVALID_PARAMS, nil)
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "ok",
		"data": gin.H{
			"data":     roles,
		},
	})
}

//添加单个角色
func AddRole(c *gin.Context) {
	appG := app.Gin{c}
	name := c.PostForm("name")
	displayName := c.PostForm("display_name")
	description := c.PostForm("description")
	sort := com.StrTo(c.PostForm("sort")).MustInt()
	valid := validation.Validation{}
	valid.Required(name, "name").Message("名称不能为空！")
	valid.Required(displayName, "displayName").Message("显示的名称不能为空！")
	valid.Required(description, "description").Message("不能为空！")
	valid.Required(sort, "sort").Message("排序不能为空!")
	roleModel := map[string]interface{}{
		"name":         name,
		"display_name": displayName,
		"description":  description,
		"sort":         sort,
	}

	if err := models.AddRole(roleModel); err != nil {
		appG.Response(http.StatusOK, e.INVALID_PARAMS, nil)
		return
	}

	appG.ResponseMsg(http.StatusOK, "角色新增成功！", nil)

}

// 查看单个角色
func GetRole(c *gin.Context) {
	appG := app.Gin{c}
	id := com.StrTo(c.Query("id")).MustInt()

	valid := validation.Validation{}
	valid.Min(id, 1, "id").Message("id必须大于1！")

	data, err := models.GetRole(id)
	if err != nil {
		appG.ResponseMsg(http.StatusOK, "角色查找失败！", nil)
		return
	}
	appG.Response(http.StatusOK, e.SUCCESS, data)
}

// 修改角色
func UpdateRole(c *gin.Context) {
	appG := app.Gin{c}
	id := com.StrTo(c.Query("id")).MustInt()
	valid := validation.Validation{}
	valid.Min(id, 1, "id").Message("ID必须大于1")

	name := c.PostForm("name")
	displayName := c.PostForm("display_name")
	description := c.PostForm("description")
	sort := com.StrTo(c.PostForm("sort")).MustInt()
	isEnabled := com.StrTo(c.PostForm("is_enabled")).MustInt()
	valid.Required(name, "name").Message("名称不能为空！")
	valid.Required(displayName, "display_name").Message("显示名称不能为空！")
	valid.Required(description, "description").Message("描述不能为空！")
	valid.Min(sort, 1, "sort").Message("排序号大于1")

	role := models.Role{
		ID:          id,
		Name:        name,
		DisplayName: displayName,
		Description: description,
		Sort:        sort,
		IsEnabled:   isEnabled,
		UpdatedAt:   time.Now(),
	}
	if err := role.UpdateRole(); err != nil {
		appG.Response(http.StatusOK, e.ERROR, err)
	}

	appG.ResponseMsg(200, "修改成功！", "")
}

// 删除角色
func DeletedRole(c *gin.Context) {
	appG := app.Gin{c}
	id := com.StrTo(c.PostForm("id")).MustInt()
	valid := validation.Validation{}
	valid.Min(id, 1, "id").Message("id必须大于1")
	if err := models.DeleteRole(id); err != nil {
		appG.ResponseMsg(200, "删除失败！", "")
	}

	appG.ResponseMsg(200, "删除失败！", "")

}

// 启用禁用角色

func IsEnabledRole(c *gin.Context) {
	appG := app.Gin{c}
	id := com.StrTo(c.PostForm("id")).MustInt()
	is_enabled := com.StrTo(c.PostForm("is_enabled")).MustInt()
	valid := validation.Validation{}
	valid.Min(id, 1, "id").Message("id必须大于1!")

	if err := models.IsEnableRole(id, is_enabled); err != nil {
		appG.ResponseMsg(500, "操作失败！", "")
	}
	appG.ResponseMsg(200, "操作成功！", "")
}

// 查找角色对应的权限

func GetRolePermissions(c *gin.Context) {
	appG := app.Gin{c}
	role_id := com.StrTo(c.Query("role_id")).MustInt()
	data := models.GetRolePermissionAll(role_id)
	appG.Response(http.StatusOK, e.SUCCESS, data)
}

// 修改角色的权限

func UpdateRolePermissions(c *gin.Context) {
	appG := app.Gin{c}

	permission_id := c.PostForm("permission_id")
	role_id := com.StrTo(c.PostForm("role_id")).MustInt()
	data := strings.Split(permission_id, ",")

	err := models.UpdatePermissionRole(role_id, data)
	if err != nil {
		appG.ResponseMsg(http.StatusInternalServerError, "修改错误！", err)
	}

	appG.ResponseMsg(http.StatusOK, "修改成功！", permission_id)
}
